//
//  GKZYHomeCourseCell.m
//  GKZY_CLASS
//
//  Created by KwokChun Cheung on 15/11/26.
//  Copyright © 2015年 eenet. All rights reserved.
//

#import "GKZYHomeCourseCell.h"
#import "xxptmobile.h"

#pragma mark - OOHomeCourseModel
@implementation GKZYHomeCourseModel
-(GKZYHomeCourseStatus)courseStatus{
    if (!_courseStatus) {
        _courseStatus = GKZYHomeCourseNone;
    }
    return _courseStatus;
}
-(NSString *)scoreForStudyMark{
    //业务需求 0为未学习,需要显示双横杆
    if ([_scoreForStudyMark isEqualToString:@"0"]) {
        return @"--";
    }
    return _scoreForStudyMark;
}
@end

#pragma mark - OOHomeCourseCell
@interface GKZYHomeCourseCell()
@property (nonatomic, strong) UIView *topArea;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *requiredLabel;
@property (nonatomic, strong) UIView *learningView;
@property (nonatomic, strong) UIView *learningRateView;
@property (nonatomic, strong) UILabel *learningLabel;

@property (nonatomic, strong) UIView *middleArea;
@property (nonatomic, strong) UIView *studyMarkView;//学习成绩
@property (nonatomic, strong) UILabel *rateStudyMarkLabel;
@property (nonatomic, strong) UILabel *scoreStudyMarkLabel;
@property (nonatomic, strong) UIView *examMarkView;//考试成绩
@property (nonatomic, strong) UILabel *rateExamMarkLabel;
@property (nonatomic, strong) UILabel *scoreExamMarkLabel;
@property (nonatomic, strong) UIView *totalMarkView;//总成绩
@property (nonatomic, strong) UILabel *rateTotalMarkLabel;
@property (nonatomic, strong) UILabel *scoreTotalMarkLabel;

@property (nonatomic, strong) UIView *bottomArea;
@property (nonatomic, strong) UILabel *courseStatusFlag;
@property (nonatomic, strong) UILabel *courseStatusWord;
@property (nonatomic, strong) UILabel *courseStatusArrow;
@end

@implementation GKZYHomeCourseCell{
    
    CGSize sizeForLearningView;
    CGSize sizeForRequiredLabel;
    
    CGFloat sizeForRate;
    UIColor *colorForRate;
    
    CGFloat sizeForScore;
    UIColor *colorForScore;
    
    CGFloat offsetForRateWord;

    CGFloat heightForTopArea;
    CGFloat heightForMiddleArea;
    CGFloat heightForBottomArea;
    
    CGFloat sizeForArrow;
    
    NSLayoutConstraint *bottomLayout;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        sizeForArrow = 16;
        
        sizeForLearningView = CGSizeMake(60, 20);
        sizeForRequiredLabel = CGSizeMake(55, 18);
        
        sizeForRate = 9;
        colorForRate = [UIColor colorWithRed:161/255.0 green:161/255.0 blue:161/255.0 alpha:1];
        sizeForScore = 13;
        colorForScore = [UIColor colorWithRed:154/255.0 green:154/255.0 blue:154/255.0 alpha:1];
        offsetForRateWord = 0.7;
        
        heightForTopArea = 70;
        heightForMiddleArea = 30;
        heightForBottomArea = 40;
        
        [self addGapLineWith:self.topArea on:LinePositionTop];
        [self addGapLineWith:self.topArea on:LinePositionBottom];
        [self addSubview:self.topArea];
        [self.topArea mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self);
            make.height.equalTo(@(heightForTopArea));
        }];
        
        [self addGapLineWith:self.middleArea on:LinePositionBottom];
        [self addSubview:self.middleArea];
        [self.middleArea mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.topArea.mas_bottom);
            make.left.right.equalTo(self.topArea);
            make.height.equalTo(@(heightForMiddleArea));
        }];
        
        [self addSubview:self.bottomArea];
        [self.bottomArea mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.middleArea.mas_bottom);
            make.left.right.equalTo(self.topArea);
            make.bottom.equalTo(self);
        }];
        
        
        
    }
    return self;
}

#pragma mark - Event response
- (void)accessExamineVC:(UITapGestureRecognizer *)tap{
    if ([self.delegate respondsToSelector:@selector(OOHomeCourseCellDelegateShouldPresentNextViewControllerWithSender:)]) {
        [self.delegate OOHomeCourseCellDelegateShouldPresentNextViewControllerWithSender:tap.view];
    }
}

#pragma mark - Setter For Property

-(void)setModel:(GKZYHomeCourseModel *)model{
    _model = model;
    
    if (model) {
        self.titleLabel.text = model.title;
        
        //必修区域
        if (model.isRequired) {
            self.requiredLabel.hidden = NO;
            self.requiredLabel.text = [NSString stringWithFormat:@"必修 %@学分",model.scoreForRequired];
            [self.requiredLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(sizeForRequiredLabel);
            }];
            [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.topArea).multipliedBy(0.7);
                make.left.equalTo(self.topArea).offset(10);
            }];
        }else{
            self.requiredLabel.hidden = YES;
            [self.requiredLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(0));
            }];
            [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.topArea);
                make.left.equalTo(self.topArea).offset(10);
            }];
        }
        
        //已学区域
        if (![model.rateForLearning isEqualToString:@""] || [model.rateForLearning intValue] > 0) {
            self.learningLabel.text = [NSString stringWithFormat:@"已学%@%%",model.rateForLearning];
            CGFloat mul = [model.rateForLearning floatValue]/100;
            [self.learningRateView mas_updateConstraints:^(MASConstraintMaker *make) {
               make.width.mas_equalTo(sizeForLearningView.width * mul);
            }];
        }else{
            self.learningLabel.text = @"未学习";
        }

        
        //成绩区域
        self.rateStudyMarkLabel.text = [NSString stringWithFormat:@"学习成绩(%@%%)",model.rateForStudyMark];
        self.scoreStudyMarkLabel.text = [NSString stringWithFormat:@"%@",model.scoreForStudyMark];
        
        self.rateExamMarkLabel.text = [NSString stringWithFormat:@"考试成绩(%@%%)",model.rateForExamMark];
        self.scoreExamMarkLabel.text = [NSString stringWithFormat:@"%@",model.scoreForExamMark];
        
        self.rateTotalMarkLabel.text = [NSString stringWithFormat:@"总成绩(%@分通过)",model.rateForTotalMark];
        self.scoreTotalMarkLabel.text = [NSString stringWithFormat:@"%@",model.scoreForTotalMark];

        
        //考试状态
        [self.bottomArea removeConstraint:bottomLayout];
        
        if (model.courseStatus == GKZYHomeCourseNone ||
            model.courseStatus == GKZYHomeCourseProcessing) {
            
        }else if (model.courseStatus == GKZYHomeCoursePass ||
                  model.courseStatus == GKZYHomeCourseRetry ||
                  model.courseStatus == GKZYHomeCourseFinish){
            
            self.height = 140;
            bottomLayout = [self.bottomArea constraintHeight:heightForBottomArea];
            [self.bottomArea addConstraint:bottomLayout];
            
            
            
            self.courseStatusArrow.hidden = [model.examUrlAble intValue] == 2;
            
            switch (model.courseStatus) {
                case GKZYHomeCoursePass:{
                    self.courseStatusFlag.text = @"\U0000F06A";
                    self.courseStatusWord.text = model.examTip;//@"您已通过课程学习, 请查看考试安排";
                    self.bottomArea.backgroundColor = [UIColor colorWithRed:143/255.0 green:203/255.0 blue:248/255.0 alpha:1];
                }break;
                case GKZYHomeCourseRetry:{
                    self.courseStatusFlag.text = @"\U0000F06A";
                    self.courseStatusWord.text = model.examTip;//@"考试结束, 未通过考试, 查看重考安排";
                    self.bottomArea.backgroundColor = [UIColor colorWithRed:254/255.0 green:172/255.0 blue:144/255.0 alpha:1];
                }break;
                case GKZYHomeCourseFinish:{
                    self.courseStatusFlag.text = @"\U0000F058";
                    self.bottomArea.backgroundColor = [UIColor colorWithRed:164/255.0 green:215/255.0 blue:165/255.0 alpha:1];
                    self.courseStatusWord.text = model.examTip;//[NSString stringWithFormat:@"恭喜您, 已顺利通过课程学习和期末考试, 获得%@学分",model.scoreForRequired];
                    self.courseStatusArrow.hidden = YES;
                }break;
                
                default:
                    break;
            }
            
        }
    }
    
}

#pragma mark - Getter For UI

#pragma mark - topArea

-(UIView *)topArea{
    if (!_topArea) {
        _topArea = ({
            UIView *view = [UIView new];
            [view addSubview:self.titleLabel];
            [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(view).multipliedBy(0.7);
                make.left.equalTo(view).offset(10);
            }];
            
            [view addSubview:self.requiredLabel];
            [self.requiredLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.titleLabel.mas_bottom).offset(8);
                make.left.equalTo(self.titleLabel);
                make.size.mas_equalTo(sizeForRequiredLabel);
            }];

            UILabel *arrow = [UILabel new];
            arrow.font = [[ResourceHelper archiveFont:FONT_FONTAWESOME withType:@"otf"] fontWithSize:sizeForArrow];
            arrow.text = @"\U0000F054";
            arrow.textColor = [UIColor lightGrayColor];
            arrow.textAlignment = NSTextAlignmentCenter;
            [arrow sizeToFit];
            [view addSubview:arrow];
            [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(view);
                make.right.equalTo(view).offset(-10);
            }];

            [view addSubview:self.learningView];
            [self.learningView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(view);
                make.right.equalTo(arrow.mas_left).offset(-10);
                make.size.mas_equalTo(sizeForLearningView);
            }];
            
            view;
        });
    }
    return _topArea;
}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = ({
            UILabel *label = [UILabel new];
            label.textColor = [UIColor blackColor];
            label.font = [UIFont systemFontOfSize:16];
            label;
        });
    }
    return _titleLabel;
}

-(UILabel *)requiredLabel{
    if (!_requiredLabel) {
        _requiredLabel = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = colorForRate;
            label.font = [UIFont systemFontOfSize:sizeForRate];
            label.layer.borderWidth = 0.8;
            label.layer.borderColor = [UIColor lightGrayColor].CGColor;
            label.layer.cornerRadius = 6;
            label;
        });
    }
    return _requiredLabel;
}

-(UIView *)learningView{
    if (!_learningView) {
        _learningView = ({
            UIView *view = [UIView new];
            view.backgroundColor = [UIColor colorWithRed:206/255.0 green:217/255.0 blue:221/255.0 alpha:1];
            view.layer.masksToBounds = YES;
            view.layer.cornerRadius = sizeForLearningView.height/2;
            
            UIView *rate = [UIView new];
            rate.backgroundColor = [UIColor colorWithRed:100/255.0 green:181/255.0 blue:246/255.0 alpha:1];
            [view addSubview:rate];
            [rate mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.left.bottom.equalTo(view);
            }];
            self.learningRateView = rate;
            
            [view addSubview:self.learningLabel];
            [self.learningLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(view);
            }];
            view;
        });
    }
    return _learningView;
}

-(UILabel *)learningLabel{
    if (!_learningLabel) {
        _learningLabel = ({
            UILabel *label = [UILabel new];
            label.font = [UIFont systemFontOfSize:sizeForRate];
            label.textColor = [UIColor whiteColor];
            label.textAlignment = NSTextAlignmentCenter;
            label;
        });
    }
    return _learningLabel;
}

#pragma mark - middleArea

-(UIView *)middleArea{
    if (!_middleArea) {
        _middleArea = ({
            UIView *view = [UIView new];
            view.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1];
            
            [view addSubview:self.studyMarkView];
            [self.studyMarkView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.left.bottom.equalTo(view);
                make.width.equalTo(view).multipliedBy(0.33);
            }];
            
            [view addSubview:self.examMarkView];
            [self.examMarkView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.equalTo(view);
                make.left.equalTo(self.studyMarkView.mas_right);
                make.width.equalTo(view).multipliedBy(0.33);
            }];
            
            [view addSubview:self.totalMarkView];
            [self.totalMarkView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.equalTo(view);
                make.left.equalTo(self.examMarkView.mas_right);
                make.width.equalTo(view).multipliedBy(0.34);
            }];
            
            view;
        });
    }
    return _middleArea;
}

-(UIView *)studyMarkView{
    if (!_studyMarkView) {
        _studyMarkView = ({
            UIView *view = [UIView new];
            
            self.rateStudyMarkLabel = [UILabel new];
            self.rateStudyMarkLabel.font = [UIFont systemFontOfSize:sizeForRate];
            self.rateStudyMarkLabel.textColor = colorForRate;
            [view addSubview:self.rateStudyMarkLabel];
            [self.rateStudyMarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(view);
                make.centerX.equalTo(view).multipliedBy(offsetForRateWord);
            }];
            
            self.scoreStudyMarkLabel = [UILabel new];
            self.scoreStudyMarkLabel.font = [UIFont systemFontOfSize:sizeForScore];
            self.scoreStudyMarkLabel.textColor = colorForScore;
            [view addSubview:self.scoreStudyMarkLabel];
            [self.scoreStudyMarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(view);
                make.left.equalTo(self.rateStudyMarkLabel.mas_right).offset(4);
            }];
            
            view;
        });
    }
    return _studyMarkView;
}

-(UIView *)examMarkView{
    if (!_examMarkView) {
        _examMarkView = ({
            UIView *view = [UIView new];
            
            self.rateExamMarkLabel = [UILabel new];
            self.rateExamMarkLabel.font = [UIFont systemFontOfSize:sizeForRate];
            self.rateExamMarkLabel.textColor = colorForRate;
            [view addSubview:self.rateExamMarkLabel];
            [self.rateExamMarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(view);
                make.centerX.equalTo(view).multipliedBy(offsetForRateWord);
            }];
            
            self.scoreExamMarkLabel = [UILabel new];
            self.scoreExamMarkLabel.font = [UIFont systemFontOfSize:sizeForScore];
            self.scoreExamMarkLabel.textColor = colorForRate;
            [view addSubview:self.scoreExamMarkLabel];
            [self.scoreExamMarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(view);
                make.left.equalTo(self.rateExamMarkLabel.mas_right).offset(4);
            }];
            
            view;
        });
    }
    return _examMarkView;
}

-(UIView *)totalMarkView{
    if (!_totalMarkView) {
        _totalMarkView = ({
            UIView *view = [UIView new];
            
            self.rateTotalMarkLabel = [UILabel new];
            self.rateTotalMarkLabel.font = [UIFont systemFontOfSize:sizeForRate];
            self.rateTotalMarkLabel.textColor = colorForRate;
            [view addSubview:self.rateTotalMarkLabel];
            [self.rateTotalMarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(view);
                make.centerX.equalTo(view).multipliedBy(offsetForRateWord);
            }];
            
            self.scoreTotalMarkLabel = [UILabel new];
            self.scoreTotalMarkLabel.font = [UIFont systemFontOfSize:sizeForScore];
            self.scoreTotalMarkLabel.textColor = colorForRate;
            [view addSubview:self.scoreTotalMarkLabel];
            [self.scoreTotalMarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(view);
                make.left.equalTo(self.rateTotalMarkLabel.mas_right).offset(4);
            }];
            
            view;
        });
    }
    return _totalMarkView;
}

#pragma mark - bottomArea

-(UIView *)bottomArea{
    if (!_bottomArea) {
        _bottomArea = ({
            UIView *view = [UIView new];
            view.layer.masksToBounds = YES;
            
            CGFloat sizeForFlag = 19;
            
            self.courseStatusFlag = [UILabel new];
            self.courseStatusFlag.font = [[ResourceHelper archiveFont:FONT_FONTAWESOME withType:@"otf"] fontWithSize:sizeForFlag];
            self.courseStatusFlag.textColor = [UIColor whiteColor];
            self.courseStatusFlag.textAlignment = NSTextAlignmentCenter;
            [view addSubview:self.courseStatusFlag];
            [self.courseStatusFlag mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(view);
                make.left.equalTo(view).offset(15);
                make.size.mas_equalTo(CGSizeMake(sizeForFlag, sizeForFlag));
            }];
            
            UILabel *arrow = [UILabel new];
            arrow.font = [[ResourceHelper archiveFont:FONT_FONTAWESOME withType:@"otf"] fontWithSize:sizeForArrow];
            arrow.text = @"\U0000F054";
            arrow.textColor = [UIColor whiteColor];
            arrow.textAlignment = NSTextAlignmentCenter;
            [arrow sizeToFit];
            [view addSubview:arrow];
            [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(view);
                make.right.equalTo(view).offset(-10);
            }];
            self.courseStatusArrow = arrow;
            
            self.courseStatusWord = [UILabel new];
            self.courseStatusWord.textColor = [UIColor whiteColor];
            self.courseStatusWord.font = [UIFont systemFontOfSize:12];
            [view addSubview:self.courseStatusWord];
            [self.courseStatusWord mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(view);
                make.left.equalTo(self.courseStatusFlag.mas_right).offset(10);
                make.right.equalTo(arrow.mas_left).priorityLow();
            }];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(accessExamineVC:)];
            [view addGestureRecognizer:tap];
            view;
        });
    }
    return _bottomArea;
}


#pragma mark - Tools Methods

typedef NS_ENUM(NSUInteger, LinePosition) {
    LinePositionTop,
    LinePositionLeft,
    LinePositionRight,
    LinePositionBottom,
};
-(void)addGapLineWith:(UIView *)view on:(LinePosition)posit{
    //用于防止重复插入
    NSInteger tagNumber = 960;
    switch (posit) {
        case LinePositionTop:
            tagNumber = 961;
            break;
        case LinePositionLeft:
            tagNumber = 962;
            break;
        case LinePositionRight:
            tagNumber = 963;
            break;
        case LinePositionBottom:
            tagNumber = 964;
            break;
        default:
            break;
    }
    UIView *exist = [view viewWithTag:tagNumber];
    if (exist) { return; }
    
    CGFloat lineSizeUnit = 0.5;
    UILabel *line = [UILabel new];
    line.backgroundColor = [UIColor lightGrayColor];
    line.tag = tagNumber;
    
    [view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        if (posit==LinePositionRight || posit==LinePositionLeft) {
            if (posit==LinePositionRight) {
                make.right.equalTo(view);
            }else if (posit==LinePositionLeft){
                make.left.equalTo(view);
            }
            make.top.equalTo(view);
            make.height.equalTo(view);
            make.width.equalTo(@(lineSizeUnit));
        }else if (posit==LinePositionTop || posit==LinePositionBottom){
            if (posit==LinePositionTop) {
                make.top.equalTo(view);
            }else if (posit==LinePositionBottom){
                make.bottom.equalTo(view);
            }
            make.height.equalTo(@(lineSizeUnit));
            make.width.equalTo(view);
        }
    }];
}
@end
